package com.industrial.operation.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.hash.Hash;
import com.industrial.common.core.utils.DateUtils;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.operation.domain.SysDataReporting;
import com.industrial.operation.mapper.CountyDeveSituationMapper;
import com.industrial.operation.service.ICountyDeveSituationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CountyDeveSituationServiceImpl implements ICountyDeveSituationService {

    @Autowired
    private CountyDeveSituationMapper countyDeveSituationMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 全国工业增加值
     *
     * @return
     */
    @Override
    public AjaxResult queryNationalIndustrialAddedValue() {
        int year = DateUtil.year(DateUtils.getNowDate()) - 1;
        SysDataReporting params = new SysDataReporting();
        params.setYear(String.valueOf(year));
        List<SysDataReporting> result = countyDeveSituationMapper.queryNationalIndustrialAddedValue(params);
        List<String> y = new ArrayList<>();
        List<Double> values = new ArrayList<>();
        for(SysDataReporting item : result){
            String district = item.getDistrict();
            String perMuGdp = item.getPerMuGdp();

            if(StringUtils.isBlank(perMuGdp)){
                values.add(Double.valueOf("0"));
            }else{
                values.add(Double.valueOf(perMuGdp));
            }

            y.add(stringRedisTemplate.opsForValue().get("area:" + district));
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("y",y);
        resultMap.put("values",values);
        return AjaxResult.success(resultMap);
    }

    /**
     * 规模以上工业增加值增速
     * @return
     */
    @Override
    public AjaxResult queryPreMuAla() {
        int year = DateUtil.year(DateUtils.getNowDate()) - 1;
        SysDataReporting params = new SysDataReporting();
        params.setYear(String.valueOf(year));
        List<SysDataReporting> result = countyDeveSituationMapper.queryPreMuAla(params);
        List<String> y = new ArrayList<>();
        List<Double> values = new ArrayList<>();
        for(SysDataReporting item : result){
            String district = item.getDistrict();
            String preMuAla = item.getPreMuAla();

            if(StringUtils.isBlank(preMuAla)){
                values.add(Double.valueOf("0"));
            }else{
                values.add(Double.valueOf(preMuAla));
            }

            y.add(stringRedisTemplate.opsForValue().get("area:" + district));
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("y",y);
        resultMap.put("values",values);
        return AjaxResult.success(resultMap);
    }

    /**
     * 制造业增值税、企业所得税等重点税收贡献情况
     * @return
     */
    @Override
    public AjaxResult queryPreMuEcPt() {
        int year = DateUtil.year(DateUtils.getNowDate()) - 1;
        SysDataReporting params = new SysDataReporting();
        params.setYear(String.valueOf(year));
        List<SysDataReporting> result = countyDeveSituationMapper.queryPreMuEcPt(params);
        List<String> y = new ArrayList<>();
        List<Double> values = new ArrayList<>();
        for(SysDataReporting item : result){
            String district = item.getDistrict();
            String preMuEcPt = item.getPreMuEcPt();

            if(StringUtils.isBlank(preMuEcPt)){
                values.add(Double.valueOf("0"));
            }else{
                values.add(Double.valueOf(preMuEcPt));
            }

            y.add(stringRedisTemplate.opsForValue().get("area:" + district));
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("y",y);
        resultMap.put("values",values);
        return AjaxResult.success(resultMap);
    }

    @Override
    public AjaxResult queryPreMuEcTec() {
        int year = DateUtil.year(DateUtils.getNowDate()) - 1;
        SysDataReporting params = new SysDataReporting();
        params.setYear(String.valueOf(year));
        List<SysDataReporting> result = countyDeveSituationMapper.queryPreMuEcTec(params);
        List<String> y = new ArrayList<>();
        List<Double> values = new ArrayList<>();
        for(SysDataReporting item : result){
            String district = item.getDistrict();
            String preMuEcTec = item.getPreMuEcTec();

            if(StringUtils.isBlank(preMuEcTec)){
                values.add(Double.valueOf("0"));
            }else{
                values.add(Double.valueOf(preMuEcTec));
            }

            y.add(stringRedisTemplate.opsForValue().get("area:" + district));
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("y",y);
        resultMap.put("values",values);
        return AjaxResult.success(resultMap);
    }

    /**
     * 规模以上工业企业数量及增幅
     *
     * @return
     */
    @Override
    public AjaxResult queryLpvGdp() {
        int year = DateUtil.year(DateUtils.getNowDate()) - 1;
        SysDataReporting params = new SysDataReporting();
        params.setYear(String.valueOf(year));
        List<SysDataReporting> result = countyDeveSituationMapper.queryLpvGdp(params);
        List<String> y = new ArrayList<>();
        List<Double> values = new ArrayList<>();
        for(SysDataReporting item : result){
            String district = item.getDistrict();
            String lpvGdp = item.getLpvGdp();

            if(StringUtils.isBlank(lpvGdp)){
                values.add(Double.valueOf("0"));
            }else{
                values.add(Double.valueOf(lpvGdp));
            }

            y.add(stringRedisTemplate.opsForValue().get("area:" + district));
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("y",y);
        resultMap.put("values",values);
        return AjaxResult.success(resultMap);
    }

    /**
     * 规模以上工业企业数量及增幅
     *
     * @return
     */
    @Override
    public AjaxResult queryLpvAvNumEmploy() {
        int year = DateUtil.year(DateUtils.getNowDate()) - 1;
        SysDataReporting params = new SysDataReporting();
        params.setYear(String.valueOf(year));
        List<SysDataReporting> result = countyDeveSituationMapper.queryLpvAvNumEmploy(params);
        List<String> y = new ArrayList<>();
        List<Double> values = new ArrayList<>();
        for(SysDataReporting item : result){
            String district = item.getDistrict();
            String lpvAvNumEmploy = item.getLpvAvNumEmploy();

            if(StringUtils.isBlank(lpvAvNumEmploy)){
                values.add(Double.valueOf("0"));
            }else{
                values.add(Double.valueOf(lpvAvNumEmploy));
            }

            y.add(stringRedisTemplate.opsForValue().get("area:" + district));
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("y",y);
        resultMap.put("values",values);
        return AjaxResult.success(resultMap);
    }

    /**
     * 工业技术改造投资增速
     *
     * @return
     */
    @Override
    public AjaxResult queryEpResearchDevelop() {
        int year = DateUtil.year(DateUtils.getNowDate()) - 1;
        SysDataReporting params = new SysDataReporting();
        params.setYear(String.valueOf(year));
        List<SysDataReporting> result = countyDeveSituationMapper.queryEpResearchDevelop(params);
        List<String> y = new ArrayList<>();
        List<Double> values = new ArrayList<>();
        for(SysDataReporting item : result){
            String district = item.getDistrict();
            String epResearchDevelop = item.getEpResearchDevelop();

            if(StringUtils.isBlank(epResearchDevelop)){
                values.add(Double.valueOf("0"));
            }else{
                values.add(Double.valueOf(epResearchDevelop));
            }

            y.add(stringRedisTemplate.opsForValue().get("area:" + district));
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("y",y);
        resultMap.put("values",values);
        return AjaxResult.success(resultMap);
    }

    /**
     * 规模以上工业企业亩均税收
     *
     * @return
     */
    @Override
    public AjaxResult queryEpOperatingRevenue() {
        int year = DateUtil.year(DateUtils.getNowDate()) - 1;
        SysDataReporting params = new SysDataReporting();
        params.setYear(String.valueOf(year));
        List<SysDataReporting> result = countyDeveSituationMapper.queryEpOperatingRevenue(params);
        List<String> y = new ArrayList<>();
        List<Double> values = new ArrayList<>();
        for(SysDataReporting item : result){
            String district = item.getDistrict();
            String epOperatingRevenue = item.getEpOperatingRevenue();

            if(StringUtils.isBlank(epOperatingRevenue)){
                values.add(Double.valueOf("0"));
            }else{
                values.add(Double.valueOf(epOperatingRevenue));
            }

            y.add(stringRedisTemplate.opsForValue().get("area:" + district));
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("y",y);
        resultMap.put("values",values);
        return AjaxResult.success(resultMap);
    }

    /**
     * 省级以上专精特新及单项冠军数量
     *
     * @return
     */
    @Override
    public AjaxResult queryPollutionEmission() {
        int year = DateUtil.year(DateUtils.getNowDate()) - 1;
        SysDataReporting params = new SysDataReporting();
        params.setYear(String.valueOf(year));
        List<SysDataReporting> result = countyDeveSituationMapper.queryPollutionEmission(params);
        List<String> y = new ArrayList<>();
        List<Double> values = new ArrayList<>();
        for(SysDataReporting item : result){
            String district = item.getDistrict();
            String pollutionEmission = item.getPollutionEmission();

            if(StringUtils.isBlank(pollutionEmission)){
                values.add(Double.valueOf("0"));
            }else{
                values.add(Double.valueOf(pollutionEmission));
            }

            y.add(stringRedisTemplate.opsForValue().get("area:" + district));
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("y",y);
        resultMap.put("values",values);
        return AjaxResult.success(resultMap);
    }

    /**
     * 制造业贷款占各项贷款余额比重
     *
     * @return
     */
    @Override
    public AjaxResult queryCompDevelopMvg() {
        int year = DateUtil.year(DateUtils.getNowDate()) - 1;
        SysDataReporting params = new SysDataReporting();
        params.setYear(String.valueOf(year));
        List<SysDataReporting> result = countyDeveSituationMapper.queryCompDevelopMvg(params);
        List<String> y = new ArrayList<>();
        List<Double> values = new ArrayList<>();
        for(SysDataReporting item : result){
            String district = item.getDistrict();
            String compDevelopMvg = item.getCompDevelopMvg();
            if(StringUtils.isBlank(compDevelopMvg)){
                values.add(Double.valueOf("0"));
            }else{
                values.add(Double.valueOf(compDevelopMvg));
            }

            y.add(stringRedisTemplate.opsForValue().get("area:" + district));
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("y",y);
        resultMap.put("values",values);
        return AjaxResult.success(resultMap);
    }

    /**
     * 规模以上工业企业R&D经费支出占营业收入比重
     *
     * @return
     */
    @Override
    public AjaxResult queryCompDevelopQr() {
        int year = DateUtil.year(DateUtils.getNowDate()) - 1;
        SysDataReporting params = new SysDataReporting();
        params.setYear(String.valueOf(year));
        List<SysDataReporting> result = countyDeveSituationMapper.queryCompDevelopQr(params);
        List<String> y = new ArrayList<>();
        List<Double> values = new ArrayList<>();
        for(SysDataReporting item : result){
            String district = item.getDistrict();
            String compDevelopQr = item.getCompDevelopQr();

            if(StringUtils.isBlank(compDevelopQr)){
                values.add(Double.valueOf("0"));
            }else{
                values.add(Double.valueOf(compDevelopQr));
            }

            y.add(stringRedisTemplate.opsForValue().get("area:" + district));
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("y",y);
        resultMap.put("values",values);
        return AjaxResult.success(resultMap);
    }

    /**
     * 高技术产业产值占规模以上工业总产值比重
     *
     * @return
     */
    @Override
    public AjaxResult queryCompDevelopEn() {
        int year = DateUtil.year(DateUtils.getNowDate()) - 1;
        SysDataReporting params = new SysDataReporting();
        params.setYear(String.valueOf(year));
        List<SysDataReporting> result = countyDeveSituationMapper.queryCompDevelopEn(params);
        List<String> y = new ArrayList<>();
        List<Double> values = new ArrayList<>();
        for(SysDataReporting item : result){
            String district = item.getDistrict();
            String compDevelopEn = item.getCompDevelopEn();

            if(StringUtils.isBlank(compDevelopEn)){
                values.add(Double.valueOf("0"));
            }else{
                values.add(Double.valueOf(compDevelopEn));
            }

            y.add(stringRedisTemplate.opsForValue().get("area:" + district));
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("y",y);
        resultMap.put("values",values);
        return AjaxResult.success(resultMap);
    }

    /**
     * 数字经济核心产业收入规模
     *
     * @return
     */
    @Override
    public AjaxResult queryCompDevelopRc() {
        int year = DateUtil.year(DateUtils.getNowDate()) - 1;
        SysDataReporting params = new SysDataReporting();
        params.setYear(String.valueOf(year));
        List<SysDataReporting> result = countyDeveSituationMapper.queryCompDevelopRc(params);
        List<String> y = new ArrayList<>();
        List<Double> values = new ArrayList<>();
        for(SysDataReporting item : result){
            String district = item.getDistrict();
            String compDevelopRc = item.getCompDevelopRc();

            if(StringUtils.isBlank(compDevelopRc)){
                values.add(Double.valueOf("0"));
            }else{
                values.add(Double.valueOf(compDevelopRc));
            }

            y.add(stringRedisTemplate.opsForValue().get("area:" + district));
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("y",y);
        resultMap.put("values",values);
        return AjaxResult.success(resultMap);
    }

    /**
     * 规模以上工业企业新产品销售收入占规模以上工业营业收入比重
     *
     * @return
     */
    @Override
    public AjaxResult queryCompDevelopTo() {
        int year = DateUtil.year(DateUtils.getNowDate()) - 1;
        SysDataReporting params = new SysDataReporting();
        params.setYear(String.valueOf(year));
        List<SysDataReporting> result = countyDeveSituationMapper.queryCompDevelopTo(params);
        List<String> y = new ArrayList<>();
        List<Double> values = new ArrayList<>();
        for(SysDataReporting item : result){
            String district = item.getDistrict();
            String compDevelopTo = item.getCompDevelopTo();

            if(StringUtils.isBlank(compDevelopTo)){
                values.add(Double.valueOf("0"));
            }else{
                values.add(Double.valueOf(compDevelopTo));
            }

            y.add(stringRedisTemplate.opsForValue().get("area:" + district));
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("y",y);
        resultMap.put("values",values);
        return AjaxResult.success(resultMap);
    }

    /**
     * 规模以上工业单位增加值能耗降低率
     *
     * @return
     */
    @Override
    public AjaxResult queryCompDevelopProduc() {
        int year = DateUtil.year(DateUtils.getNowDate()) - 1;
        SysDataReporting params = new SysDataReporting();
        params.setYear(String.valueOf(year));
        List<SysDataReporting> result = countyDeveSituationMapper.queryCompDevelopProduc(params);
        List<String> y = new ArrayList<>();
        List<Double> values = new ArrayList<>();
        for(SysDataReporting item : result){
            String district = item.getDistrict();
            String compDevelopProduc = item.getCompDevelopProduc();

            if(StringUtils.isBlank(compDevelopProduc)){
                values.add(Double.valueOf("0"));
            }else{
                values.add(Double.valueOf(compDevelopProduc));
            }

            y.add(stringRedisTemplate.opsForValue().get("area:" + district));
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("y",y);
        resultMap.put("values",values);
        return AjaxResult.success(resultMap);
    }

    /**
     * PM2.5平均浓度及同比改善率
     *
     * @return
     */
    @Override
    public AjaxResult queryInnovLeadRdOr() {
        int year = DateUtil.year(DateUtils.getNowDate()) - 1;
        SysDataReporting params = new SysDataReporting();
        params.setYear(String.valueOf(year));
        List<SysDataReporting> result = countyDeveSituationMapper.queryInnovLeadRdOr(params);
        List<String> y = new ArrayList<>();
        List<Double> values = new ArrayList<>();
        for(SysDataReporting item : result){
            String district = item.getDistrict();
            String innovLeadRdOr = item.getInnovLeadRdOr();

            if(StringUtils.isBlank(innovLeadRdOr)){
                values.add(Double.valueOf("0"));
            }else{
                values.add(Double.valueOf(innovLeadRdOr));
            }

            y.add(stringRedisTemplate.opsForValue().get("area:" + district));
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("y",y);
        resultMap.put("values",values);
        return AjaxResult.success(resultMap);
    }

    /**
     * 省级以上绿色工业园区、工厂和供应链数量
     *
     * @return
     */
    @Override
    public AjaxResult queryInnovLeadIp() {
        int year = DateUtil.year(DateUtils.getNowDate()) - 1;
        SysDataReporting params = new SysDataReporting();
        params.setYear(String.valueOf(year));
        List<SysDataReporting> result = countyDeveSituationMapper.queryInnovLeadIp(params);
        List<String> y = new ArrayList<>();
        List<Double> values = new ArrayList<>();
        for(SysDataReporting item : result){
            String district = item.getDistrict();
            String innovLeadIp = item.getInnovLeadIp();

            if(StringUtils.isBlank(innovLeadIp)){
                values.add(Double.valueOf("0"));
            }else{
                values.add(Double.valueOf(innovLeadIp));
            }

            y.add(stringRedisTemplate.opsForValue().get("area:" + district));
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("y",y);
        resultMap.put("values",values);
        return AjaxResult.success(resultMap);
    }

    /**
     * 绿色贷款余额占各项贷款余额比重
     *
     * @return
     */
    @Override
    public AjaxResult queryInnovLeadIs() {
        int year = DateUtil.year(DateUtils.getNowDate()) - 1;
        SysDataReporting params = new SysDataReporting();
        params.setYear(String.valueOf(year));
        List<SysDataReporting> result = countyDeveSituationMapper.queryInnovLeadIs(params);
        List<String> y = new ArrayList<>();
        List<Double> values = new ArrayList<>();
        for(SysDataReporting item : result){
            String district = item.getDistrict();
            String innovLeadIs = item.getInnovLeadIs();

            if(StringUtils.isBlank(innovLeadIs)){
                values.add(Double.valueOf("0"));
            }else{
                values.add(Double.valueOf(innovLeadIs));
            }

            y.add(stringRedisTemplate.opsForValue().get("area:" + district));
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("y",y);
        resultMap.put("values",values);
        return AjaxResult.success(resultMap);
    }
}
